package servlet;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;


import javabean.Factory;
import javabean.book.Book;
import javabean.book.BookComment;
import javabean.user.User;

public class BookInfoServlet extends BaseServlet{

	//初始化Servlet
    final public void init(ServletConfig config)
                throws ServletException{
    }
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4194629595765316697L;

	@Override
	protected void service() throws Exception {
		String uid = (String)request.getSession().getAttribute("uid");
		String bid = request.getParameter("bid");
		String path = "/";
		boolean isRole = false;
		if(bid!=null && !"".equals(bid)){
				List<Book> all = null;
				List<BookComment> bcAll = null;
			try{
				all = Factory.getBookDAOInstance().queryAllBookByBid(bid);
				bcAll = Factory.getBookDAOInstance().queryBookCommentByBid(bid);
				request.setAttribute("bookInfo",all);
				request.setAttribute("bookComment",bcAll);
				path = "book/book_info.jsp";
			}catch(Exception ex){
			}
			if(uid!=null){
				List<User> usAll = null;
				boolean isSendDebit = false;
				try{
					usAll = Factory.getUserDAOInstance().queryAllByUidOrEmail(uid);
					User user =  (User)usAll.get(0);
					if("1".equals(user.getRole())){
						isRole = true;
					}
					if(Factory.getTipsDAOInstance().isSendDebit(bid, uid)){
						isSendDebit = true;
					}
				}catch(Exception ex){
					
				}
				request.setAttribute("isSendDebit",isSendDebit);
				request.setAttribute("isRole", new Boolean(isRole));
			}
		}
		//跳转
		RequestDispatcher dispatcher = request.getRequestDispatcher(path);
		dispatcher.forward(request, response) ;
	}
}
